package com.lazy.bench;

import java.util.Properties; 

import org.apache.log4j.Logger;

import com.lazy.lib.util.ConfigUtil;

/**
 * 下级服务登陆参数配置类
 * 
 * @author grind
 * 
 */
public class AppCfg {
    private static Logger logger = Logger.getLogger(AppCfg.class);
    private final static Properties properties = ConfigUtil.loadProperties("conf.properties");

    // 全局唯一的对象
    private static AppCfg cfg;
    
    // 从配置文件中读取
    // ### server
    public String serverIp="";
    public int serverPort=9100;

    // ### client
    public int clientMaxThreads=1000;
    public int clientTotalPack=300;
    // # content size(bytes)
    public int clientPackSize=20;
    // # send interval(ms)
    public int clientSendInterval=10;
    
    public String clientPackContent="";
    // ///////////////////
    //
    public boolean debugEnable = true;

    // //////////////////////////////////////////////////////
    // functions
    /**
     * avoid other to create it
     */
    private AppCfg(){

    }

    /**
     * 获取屈居参数
     * 
     * @return
     */
    public static AppCfg getCfg(){
        if(cfg == null){
            cfg = new AppCfg();
            cfg.init(properties);
        }
        return cfg;
    }

    /**
     * 从配置文件中初始化参数
     * 
     * @param properties
     */
    public void init(Properties properties){
        try{
            cfg.serverIp = properties.getProperty("server.ip");
            cfg.serverPort = Integer.valueOf(properties.getProperty("server.port", "9102"));

            cfg.clientMaxThreads = Integer.valueOf(properties.getProperty("client.max_threads", "100"));
            cfg.clientTotalPack = Integer.valueOf(properties.getProperty("client.total_pack", "300"));
            cfg.clientPackSize= Integer.valueOf(properties.getProperty("client.pack_size", "20"));
            cfg.clientSendInterval = Integer.valueOf(properties.getProperty("client.send_interval", "20"));

            StringBuilder sb = new StringBuilder();
            for (int i=0; i< clientPackSize; i++) {
                sb.append("a");
            }
            clientPackContent= sb.toString();
            
        }catch(Exception e){
            e.printStackTrace();
        }
    }
}
